package com.jarvan.service.impl;

import com.jarvan.enums.EndorseEnum;
import com.jarvan.model.Endorse;
import com.jarvan.repository.EndorseRepository;
import com.jarvan.service.EndorseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EndorseServiceImpl implements EndorseService {

    @Autowired
    private EndorseRepository repository;

    @Override
    public Endorse save(Endorse endorse) {
        return repository.save(endorse);
    }

    @Override
    public Page<Endorse> findByStatus(Integer endorseStatus, Pageable pageable) {
        return repository.findAllByEndorseStatus(endorseStatus, pageable);
    }

    @Override
    public Endorse findByEndorseId(Integer endorseId) {
        return repository.findById(endorseId).orElse(null);
    }

    @Override
    public Endorse findbyTicketId(String ticketId) {
        return repository.findByTicketId(ticketId);
    }

    @Override
    public List<Endorse> findByBuyerIdAndStatus(Integer buyerId, Integer endorseStatus) {
        return repository.findAllByBuyerIdAndEndorseStatus(buyerId, endorseStatus);
    }

    @Override
    public Endorse deleteEndorse(Integer endorseId) {
        Endorse endorse = this.findByEndorseId(endorseId);
        endorse.setEndorseStatus(EndorseEnum.ENDORSE_DELETE.getCode());
        return repository.save(endorse);
    }
}
